﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DamageCalculator.Classes;
using System.Windows.Forms.DataVisualization.Charting;

namespace DamageCalculator.SubForms
{
    public partial class CharacterView : Form
    {
        Character Char;
        List<Encounter> EncounterList;
        CharacterResults CR;

        public CharacterView(Character inputchar)
        {
            InitializeComponent();
            this.Char = inputchar;
            this.Lbl_PlayerName.Text = this.Char.Name;
            this.EncounterList = new List<Encounter>();
            foreach (Encounter E in this.Char.Encounters)
            {
                this.Lst_Encounters.Items.Add(E.ToString());
                this.EncounterList.Add(E);
            }
            this.CR = new CharacterResults(this.Char.Name);
            this.CR.Recalculate(this.EncounterList);

            this.Lst_Stats.Items.Clear();
            this.Lst_Stats.Items.AddRange(this.CR.generateStatisics().ToArray());
            this.GenerateGraph(this.Cmb_GraphSelection.Text, this.Cmb_Pie_Bar.Text);
        }

        private void GenerateGraph(String GraphValues, String GraphType)
        {
            this.chart1.Legends.Clear();
            this.chart1.Series.Clear();
            this.chart1.ChartAreas.Clear();
            this.chart1.Titles.Clear();

            switch (GraphType)
            {
                case "Column":
                    switch(GraphValues)
                    {
                        case "Damage-Target":
                            ChartArea DTC_CA = new ChartArea("DamageByTarget_Col");
                            this.chart1.ChartAreas.Add(DTC_CA);

                            Title DTC_T = new Title("Damage Done by Target", Docking.Top);
                            this.chart1.Titles.Add(DTC_T);

                            Series DTC_S = new Series("Damage Done");
                            DTC_S.Color = Color.Red;
                            DTC_S.XValueType = ChartValueType.String;
                            DTC_S.YValueType = ChartValueType.Double;

                            foreach (String TargetDamaged in this.CR.DamageDonebyTarget.Keys)
                            {
                                DataPoint DTC_DP = new DataPoint(DTC_S);
                                DTC_DP.SetValueXY(TargetDamaged, this.CR.DamageDonebyTarget[TargetDamaged]);
                                DTC_S.Points.Add(DTC_DP);
                            }

                            this.chart1.Series.Add(DTC_S);
                            break;
                            
                        case "Damage-Skill":
                            ChartArea DSC_CA = new ChartArea("DamageBySkill_Col");
                            this.chart1.ChartAreas.Add(DSC_CA);

                            Title DSC_T = new Title("Damage Done by Skill", Docking.Top);
                            this.chart1.Titles.Add(DSC_T);

                            Series DSC_S = new Series("Damage Done");
                            DSC_S.Color = Color.Red;
                            DSC_S.XValueType = ChartValueType.String;
                            DSC_S.YValueType = ChartValueType.Double;

                            foreach (String DamagingSkill in this.CR.DamageDonebySkill.Keys)
                            {
                                DataPoint DSC_DP = new DataPoint(DSC_S);
                                DSC_DP.SetValueXY(DamagingSkill, this.CR.DamageDonebySkill[DamagingSkill]);
                                DSC_S.Points.Add(DSC_DP);
                            }

                            this.chart1.Series.Add(DSC_S);
                            break;

                        case "Damage-Type":
                            ChartArea DTYC_CA = new ChartArea("DamageByType_Col");
                            this.chart1.ChartAreas.Add(DTYC_CA);

                            Title DTYC_T = new Title("Damage Done by Type", Docking.Top);
                            this.chart1.Titles.Add(DTYC_T);

                            Series DTYC_S = new Series("Damage Done");
                            DTYC_S.Color = Color.Red;
                            DTYC_S.XValueType = ChartValueType.String;
                            DTYC_S.YValueType = ChartValueType.Double;

                            foreach (String DamageType in this.CR.DamageDonebyType.Keys)
                            {
                                DataPoint DTYC_DP = new DataPoint(DTYC_S);
                                DTYC_DP.SetValueXY(DamageType, this.CR.DamageDonebyType[DamageType]);
                                DTYC_S.Points.Add(DTYC_DP);
                            }

                            this.chart1.Series.Add(DTYC_S);
                            break;

                        case "Healing-Target":
                            ChartArea HTC_CA = new ChartArea("HealingByTarget_Col");
                            this.chart1.ChartAreas.Add(HTC_CA);

                            Title HTC_T = new Title("Healing Done by Target", Docking.Top);
                            this.chart1.Titles.Add(HTC_T);

                            Series HTC_S = new Series("Healing Done");
                            HTC_S.Color = Color.Blue;
                            HTC_S.XValueType = ChartValueType.String;
                            HTC_S.YValueType = ChartValueType.Double;

                            foreach (String HealedTarget in this.CR.HealingDonebyTarget.Keys)
                            {
                                DataPoint HTC_DP = new DataPoint(HTC_S);
                                HTC_DP.SetValueXY(HealedTarget, this.CR.HealingDonebyTarget[HealedTarget]);
                                HTC_S.Points.Add(HTC_DP);
                            }

                            this.chart1.Series.Add(HTC_S);
                            break;

                        case "Healing-Skill":
                            ChartArea HSC_CA = new ChartArea("HealingBySkill_Col");
                            this.chart1.ChartAreas.Add(HSC_CA);

                            Title HSC_T = new Title("Healing Done by Skill", Docking.Top);
                            this.chart1.Titles.Add(HSC_T);

                            Series HSC_S = new Series("Healing Done");
                            HSC_S.Color = Color.Blue;
                            HSC_S.XValueType = ChartValueType.String;
                            HSC_S.YValueType = ChartValueType.Double;

                            foreach (String HealingSkill in this.CR.HealingDonebySkill.Keys)
                            {
                                DataPoint P = new DataPoint(HSC_S);
                                P.SetValueXY(HealingSkill, this.CR.HealingDonebySkill[HealingSkill]);
                                HSC_S.Points.Add(P);
                            }

                            this.chart1.Series.Add(HSC_S);
                            break;


                    }
                    break;

                case "Pie":
                    switch (GraphValues)
                    {

                    }
                    break;
            }
        }

        private void Cmb_GraphSelection_TextUpdate(object sender, EventArgs e)
        {
            this.GenerateGraph(this.Cmb_GraphSelection.Text, this.Cmb_Pie_Bar.Text);
        }

        private void Chk_FullEncounters_CheckedChanged(object sender, EventArgs e)
        {
            if (this.Chk_FullEncounters.Checked)
            {
                this.CR.Recalculate(this.Char.Encounters);
            }
            else
            {
                List<Encounter> temp = new List<Encounter>();
                for (int i = 0; i < this.Lst_Encounters.Items.Count; i++)
                {
                    if (this.Lst_Encounters.GetSelected(i))
                    {
                        foreach (Encounter E in this.Char.Encounters)
                        {
                                if (this.Lst_Encounters.Items[i].ToString() == E.ToString())
                                {
                                    temp.Add(E);
                                    break;
                                }
                        }
                    }
                }
                this.CR.Recalculate(temp);
            }

            this.Lst_Stats.Items.Clear();
            this.Lst_Stats.Items.AddRange(this.CR.generateStatisics().ToArray());
            this.GenerateGraph(this.Cmb_GraphSelection.Text, this.Cmb_Pie_Bar.Text);
        }

        private void Lst_Encounters_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<Encounter> temp = new List<Encounter>();
            for (int i = 0; i < this.Lst_Encounters.Items.Count; i++)
            {
                if (this.Lst_Encounters.GetSelected(i))
                {
                    foreach (Encounter E in this.Char.Encounters)
                    {
                        if (this.Lst_Encounters.Items[i].ToString() == E.ToString())
                        {
                            temp.Add(E);
                            break;
                        }
                    }
                }
            }
            this.CR.Recalculate(temp);
            this.Lst_Stats.Items.Clear();
            this.Lst_Stats.Items.AddRange(this.CR.generateStatisics().ToArray());
            this.GenerateGraph(this.Cmb_GraphSelection.Text, this.Cmb_Pie_Bar.Text);
        }
    }
}
